﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALComment
    /// </summary>
    public partial class DALComment
    {
        public DALComment()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int commentid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbComment");
            strSql.Append(" where commentid=@commentid");
            SqlParameter[] parameters = {
					new SqlParameter("@commentid", SqlDbType.Int,4)
			};
            parameters[0].Value = commentid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbComment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbComment(");
            strSql.Append("memberid,types,proid,ipadd,updatetime,states,replayid,replay,replaytime,content,title,score,visible,isnew)");
            strSql.Append(" values (");
            strSql.Append("@memberid,@types,@proid,@ipadd,@updatetime,@states,@replayid,@replay,@replaytime,@content,@title,@score,@visible,@isnew)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@memberid", SqlDbType.Int,4),
					new SqlParameter("@types", SqlDbType.Int,4),
					new SqlParameter("@proid", SqlDbType.Int,4),
					new SqlParameter("@ipadd", SqlDbType.NVarChar,150),
					new SqlParameter("@updatetime", SqlDbType.DateTime),
					new SqlParameter("@states", SqlDbType.Bit,1),
					new SqlParameter("@replayid", SqlDbType.NVarChar,150),
					new SqlParameter("@replay", SqlDbType.NVarChar,2000),
					new SqlParameter("@replaytime", SqlDbType.DateTime),
					new SqlParameter("@content", SqlDbType.NVarChar,2000),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@score", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@isnew", SqlDbType.Bit,1)};
            parameters[0].Value = model.memberid;
            parameters[1].Value = model.types;
            parameters[2].Value = model.proid;
            parameters[3].Value = model.ipadd;
            parameters[4].Value = model.updatetime;
            parameters[5].Value = model.states;
            parameters[6].Value = model.replayid;
            parameters[7].Value = model.replay;
            parameters[8].Value = model.replaytime;
            parameters[9].Value = model.content;
            parameters[10].Value = model.title;
            parameters[11].Value = model.score;
            parameters[12].Value = model.visible;
            parameters[13].Value = model.isnew;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbComment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbComment set ");
            strSql.Append("memberid=@memberid,");
            strSql.Append("types=@types,");
            strSql.Append("proid=@proid,");
            strSql.Append("ipadd=@ipadd,");
            strSql.Append("updatetime=@updatetime,");
            strSql.Append("states=@states,");
            strSql.Append("replayid=@replayid,");
            strSql.Append("replay=@replay,");
            strSql.Append("replaytime=@replaytime,");
            strSql.Append("content=@content,");
            strSql.Append("title=@title,");
            strSql.Append("score=@score,");
            strSql.Append("visible=@visible,");
            strSql.Append("isnew=@isnew");
            strSql.Append(" where commentid=@commentid");
            SqlParameter[] parameters = {
					new SqlParameter("@memberid", SqlDbType.Int,4),
					new SqlParameter("@types", SqlDbType.Int,4),
					new SqlParameter("@proid", SqlDbType.Int,4),
					new SqlParameter("@ipadd", SqlDbType.NVarChar,150),
					new SqlParameter("@updatetime", SqlDbType.DateTime),
					new SqlParameter("@states", SqlDbType.Bit,1),
					new SqlParameter("@replayid", SqlDbType.NVarChar,150),
					new SqlParameter("@replay", SqlDbType.NVarChar,2000),
					new SqlParameter("@replaytime", SqlDbType.DateTime),
					new SqlParameter("@content", SqlDbType.NVarChar,2000),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@score", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@isnew", SqlDbType.Bit,1),
					new SqlParameter("@commentid", SqlDbType.Int,4)};
            parameters[0].Value = model.memberid;
            parameters[1].Value = model.types;
            parameters[2].Value = model.proid;
            parameters[3].Value = model.ipadd;
            parameters[4].Value = model.updatetime;
            parameters[5].Value = model.states;
            parameters[6].Value = model.replayid;
            parameters[7].Value = model.replay;
            parameters[8].Value = model.replaytime;
            parameters[9].Value = model.content;
            parameters[10].Value = model.title;
            parameters[11].Value = model.score;
            parameters[12].Value = model.visible;
            parameters[13].Value = model.isnew;
            parameters[14].Value = model.commentid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int commentid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbComment ");
            strSql.Append(" where commentid=@commentid");
            SqlParameter[] parameters = {
					new SqlParameter("@commentid", SqlDbType.Int,4)
			};
            parameters[0].Value = commentid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string commentidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbComment ");
            strSql.Append(" where commentid in (" + commentidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbComment GetModel(int commentid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 commentid,memberid,types,proid,ipadd,updatetime,states,replayid,replay,replaytime,content,title,score,visible,isnew from tbComment ");
            strSql.Append(" where commentid=@commentid");
            SqlParameter[] parameters = {
					new SqlParameter("@commentid", SqlDbType.Int,4)
			};
            parameters[0].Value = commentid;

            Gift.Model.tbComment model = new Gift.Model.tbComment();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["commentid"] != null && ds.Tables[0].Rows[0]["commentid"].ToString() != "")
                {
                    model.commentid = int.Parse(ds.Tables[0].Rows[0]["commentid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["memberid"] != null && ds.Tables[0].Rows[0]["memberid"].ToString() != "")
                {
                    model.memberid = int.Parse(ds.Tables[0].Rows[0]["memberid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["types"] != null && ds.Tables[0].Rows[0]["types"].ToString() != "")
                {
                    model.types = int.Parse(ds.Tables[0].Rows[0]["types"].ToString());
                }
                if (ds.Tables[0].Rows[0]["proid"] != null && ds.Tables[0].Rows[0]["proid"].ToString() != "")
                {
                    model.proid = int.Parse(ds.Tables[0].Rows[0]["proid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ipadd"] != null && ds.Tables[0].Rows[0]["ipadd"].ToString() != "")
                {
                    model.ipadd = ds.Tables[0].Rows[0]["ipadd"].ToString();
                }
                if (ds.Tables[0].Rows[0]["updatetime"] != null && ds.Tables[0].Rows[0]["updatetime"].ToString() != "")
                {
                    model.updatetime = DateTime.Parse(ds.Tables[0].Rows[0]["updatetime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["states"] != null && ds.Tables[0].Rows[0]["states"].ToString() != "")
                {
                    model.states = bool.Parse(ds.Tables[0].Rows[0]["states"].ToString());
                }
                if (ds.Tables[0].Rows[0]["replayid"] != null && ds.Tables[0].Rows[0]["replayid"].ToString() != "")
                {
                    model.replayid = ds.Tables[0].Rows[0]["replayid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["replay"] != null && ds.Tables[0].Rows[0]["replay"].ToString() != "")
                {
                    model.replay = ds.Tables[0].Rows[0]["replay"].ToString();
                }
                if (ds.Tables[0].Rows[0]["replaytime"] != null && ds.Tables[0].Rows[0]["replaytime"].ToString() != "")
                {
                    model.replaytime = DateTime.Parse(ds.Tables[0].Rows[0]["replaytime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "")
                {
                    model.content = ds.Tables[0].Rows[0]["content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["title"] != null && ds.Tables[0].Rows[0]["title"].ToString() != "")
                {
                    model.title = ds.Tables[0].Rows[0]["title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["score"] != null && ds.Tables[0].Rows[0]["score"].ToString() != "")
                {
                    model.score = int.Parse(ds.Tables[0].Rows[0]["score"].ToString());
                }
                if (ds.Tables[0].Rows[0]["visible"] != null && ds.Tables[0].Rows[0]["visible"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["visible"].ToString() == "1") || (ds.Tables[0].Rows[0]["visible"].ToString().ToLower() == "true"))
                    {
                        model.visible = true;
                    }
                    else
                    {
                        model.visible = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["isnew"] != null && ds.Tables[0].Rows[0]["isnew"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isnew"].ToString() == "1") || (ds.Tables[0].Rows[0]["isnew"].ToString().ToLower() == "true"))
                    {
                        model.isnew = true;
                    }
                    else
                    {
                        model.isnew = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select commentid,memberid,types,proid,");
            strSql.Append(" (select goodsName from tbGoods where goodsId = tbComment.proId) as goodsName,");
            strSql.Append(" ipadd,updatetime,isnull(states,0) as states, replayid,replay,replaytime,content,title,score,visible,isnew ");
            strSql.Append(" FROM tbComment where memberId in (select memberid from tbMember  ) ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by updatetime desc ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" commentid,memberid,types,proid,ipadd,updatetime,states,replayid,replay,replaytime,content,title,score,visible,isnew ");
            strSql.Append(" FROM tbComment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbComment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.commentid desc");
            }
            strSql.Append(")AS Row, T.*  from tbComment T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbComment";
            parameters[1].Value = "commentid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

